#include "Surface.h"

Surface::Surface(LPDIRECT3DDEVICE9 d3ddv, LPWSTR FileName)
{
	SetDevice(d3ddv);
	_fileName = FileName;
	Load(_fileName);
}
Surface::~Surface()
{
	if (_Surface)
		_Surface->Release();
}

void Surface::Load(LPWSTR FileName)
{
	 

	D3DXIMAGE_INFO info; 

	HRESULT result = D3DXGetImageInfoFromFile(FileName, &info);
	if (result != D3D_OK)
	{
		trace(L"[ERROR] Failed to get image info ");
		return;
	}

	_d3ddv->CreateOffscreenPlainSurface(
		info.Width,				//width of the Surface
		info.Height,			//height of the Surface
		D3DFMT_X8R8G8B8,		//Surface format
		D3DPOOL_DEFAULT,		//memory pool to use
		&_Surface,				//pointer to the Surface
		NULL);					//reserved (always NULL)

	result = D3DXLoadSurfaceFromFile(
		_Surface, 					// destanation Surface
		NULL,						// destination palette	
		NULL,						// destination rectangle 
		FileName,					// source filename
		NULL,						// source rectangle
		D3DX_DEFAULT, 				// filter image
		D3DCOLOR_XRGB(0,0,0),		// transparency (0 = none)
		NULL);						// source image info (usually NULL)

	if (result != D3D_OK)
	{
		trace(L"[ERROR] D3DXLoadSurfaceFromFile() failed");
		return;
	}
}

void Surface::Draw(LPDIRECT3DSURFACE9 BackBuffer, const RECT* sRect, const RECT* dRect)
{
	_d3ddv->StretchRect(
		this->_Surface,		// from , gameworld 
		sRect,				// which portion?
		BackBuffer,		// to , backbuffer
		dRect,				// which portion?
		D3DTEXF_NONE);
}